$.ajax({
    method: "get",
    url: "http://192.168.50.142:81/selectnewgouwuche",
    success: function(data) {
        let dataList = JSON.parse(data);
        //let template = $(".oTr");
        dataList.forEach((item) => {
            let newttr =
                `<tr class="oTr">
                <td><img src="${item.tupian}" class="uuimg"></td>
                <td class="onetext">${item.foodname}</td>
                <td class="count">
                    <button type="button" class="priv">-</button>
                    <span class="goodsnums">1</span>
                    <button type="button" class="next">+</button>
                </td>
                <td>￥<span class="onegoods">${item.danjia}</span></td>
                <td>￥<span class="smallcountgoods">${item.danjia}</span></td>
                <td><input type="button" name="" id="" value="删除" class="delgoods" /><span class="usergoodsid" style="display: none;">${item.Id}</span></td>
            </tr>`;
            $(".moneylast").before(newttr);
        })


        //计算总数量数量钱=============================
        function fun() {
            //数量和总计
            let btnnum = document.querySelector("#countgoods");
            let btnmney = document.querySelector("#countmoneys");
            //起始值
            let countone = 0;
            let counttwo = 0;
            //数量的长度
            let countnumber = document.querySelectorAll("tbody .goodsnums");
            for (let i = 0; i < countnumber.length; i++) {
                let o = countnumber[i].innerHTML;
                countone += Number(o);
            }
            btnnum.innerHTML = countone;
            //钱的长度
            let countusmoney = document.querySelectorAll("tbody .smallcountgoods");
            for (let i = 0; i < countusmoney.length; i++) {
                let o = countusmoney[i].innerHTML;
                counttwo += Number(o);
            }
            btnmney.innerHTML = counttwo;
        }
        fun();

        //添加数量并计算============== 
        let btnnext = document.querySelectorAll("tbody .next");
        for (let i = 0; i < btnnext.length; i++) {
            btnnext[i].onclick = function() {
                //当前行的数量  
                let thisnum = this.previousElementSibling.innerHTML;
                this.previousElementSibling.innerHTML = Number(thisnum) + 1;
                //获取当前行的单价
                let thisonemoney = this.parentNode.nextElementSibling.firstElementChild.innerHTML;
                //获取当前行的小计
                let thiscountmoney = this.parentNode.nextElementSibling.nextElementSibling.firstElementChild;
                let money = Number(this.previousElementSibling.innerHTML) * Number(thisonemoney);
                thiscountmoney.innerHTML = money;
                fun();
            }
        }
        //减少数量并计算==============
        let btnpriv = document.querySelectorAll("tbody .priv");
        for (let i = 0; i < btnpriv.length; i++) {
            btnpriv[i].onclick = function() {
                //当前行的数量  
                let thisnum = this.nextElementSibling.innerHTML;
                thisnum = Number(thisnum) - 1;
                if (thisnum <= 0) {
                    thisnum = 0;
                }
                this.nextElementSibling.innerHTML = thisnum;
                //获取当前行的单价
                let thisonemoney = this.parentNode.nextElementSibling.firstElementChild.innerHTML;
                //获取当前行的小计
                let thiscountmoney = this.parentNode.nextElementSibling.nextElementSibling.firstElementChild;
                let money = Number(this.nextElementSibling.innerHTML) * Number(thisonemoney);
                thiscountmoney.innerHTML = money;
                fun();
            }
        }
        //删除=====================================
        let delgoods = document.querySelectorAll("tbody .delgoods");
        console.log(delgoods.length);
        for (let k = 0; k < delgoods.length; k++) {
            delgoods[k].onclick = function() {
                let delids = Number(this.nextElementSibling.innerHTML);
                alert(delids);
                if (confirm("你确定要删除吗？")) {
                    $.ajax({
                        method: "get",
                        url: "http://192.168.50.142:81/usersgoodsid",
                        data: {
                            id: delids
                        },
                        success(data) {
                            if (data === "success") {
                                alert("删除成功");
                                //history.go(0);
                                location.href = "http://192.168.50.142/MIDO/gouwuche.html";
                            }
                            if (data === "error") {
                                alert("删除失败");
                            }
                        }
                    })
                }
            }
        }
        //点击结算后
        $(".obtn").click(function() {
            let aaa = $("#countgoods").html();
            let aab = $("#countmoneys").html();
            localStorage.setItem("onea", aaa);
            localStorage.setItem("oneb", aab);
            location.href = "http://192.168.50.142/MIDO/dingdan.html";
        })

    }
})